﻿
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Models.Common;
using Models.JsonResultModel;
using ServiceWeb.DBGateway;
using Models.WebAdmin;
using ServiceWeb.WebAdmin;
using Microsoft.EntityFrameworkCore;
using ServiceWeb.DBBase;
using UtilsBase;
using UtilsORM;
using UtilsORM.MySqlData;
using UtilsORM.MySqlData;
using GatewayServer.Common;

namespace GatewayServer.Controllers.@base
{
    public class BsLanguageConfigItemController : BaseMagerController
    {
        EFYihongIotContext ef = new EFYihongIotContext();


        public IActionResult list(IFormCollection FC, HttpReqModel httpRequest, iot_device reqDevice, iot_product reqProduct, int PageIndex = 1, int PageSize = 15, string type = "1")
        {
            ViewData["reqDevice"] = reqDevice;
            ViewData["reqProduct"] = reqProduct;
            ViewBag.d_gw_d_guid = httpRequest.gwdguid;
            List<SugarParameter> sugarParameters = new List<SugarParameter>();
            int totalRecord = 0;
            string sqlwhere = " ";
            if (!string.IsNullOrEmpty(httpRequest.name))
            {
                sqlwhere = sqlwhere + " and c_name like CONCAT('%',@name,'%') ";
                sugarParameters.Add(new SugarParameter("name", httpRequest.name));
            }


            if (!string.IsNullOrEmpty(FC["hid_pageIndex"]))
            {
                PageIndex = int.Parse(FC["hid_pageIndex"].ToString());

            }

            string sql = "select *,(SELECT GROUP_CONCAT(l_name SEPARATOR ', ')  FROM bs_language\r\ninner join bs_language_config_item on ci_l_guid=l_guid where ci_c_guid=c_guid) lname  from   bs_language_config   where 1=1 " + sqlwhere;
            DataTable dt = MySqlYihongIot.getPageDataTable(PageIndex, PageSize, sql, sugarParameters, "c_id desc", ref totalRecord);
            ViewData["DataList"] = dt;
            ViewBag.PageSize = PageSize;
            ViewBag.RecordCount = totalRecord;//总条数
            ViewBag.PageCount = totalRecord % PageSize == 0 ? totalRecord / PageSize : totalRecord / PageSize + 1;//分的页数
            ViewBag.PageIndex = PageIndex;//当前页码

            return View(httpRequest);
        }
        public IActionResult edit(HttpReqModel httpReqModel)
        {
            if (httpReqModel.order == "upd")
            {
                ViewBag.edit_guid = httpReqModel.edit_guid.ToString();
            }
            else
            {
                ViewBag.edit_guid = Guid.NewGuid().ToString();
            }
            ViewBag.order = httpReqModel.order;

            return View();
        }

        public RespResultData saveAll(IFormCollection FC, HttpReqModel httpRequest, bs_language language, bs_language_config config, bs_language_config_item item)
        {
            config.c_guid = httpRequest.edit_guid;

            new DBBsLanguageConfig().saveAll(config);

            if (!string.IsNullOrEmpty(language.l_guid))
            {
                item.ci_guid = Guid.NewGuid().ToString();
                item.ci_l_guid = language.l_guid;
                item.ci_c_guid = config.c_guid;

                new DBBsLanguageConfig().saveItem(item);
            }
            return RespResult.Sucess();
        }

        public RespResultData getLanguages(HttpReqModel httpRequest)
        {
            return RespResult.Sucess(new DBBsLanguage().getLanguages());
        }
        public RespResultData getLanguagesConfigItemsForDataTable(HttpReqModel httpRequest)
        {
            DataTable dt = new DBBsLanguageConfig().getLanguagesConfigItemsForDataTable(httpRequest.guid);
            RespResultData r = RespResult.Sucess(dt);
            return r;
        }
        public RespResultData del(IFormCollection FC, HttpReqModel httpReqModel)
        {
            EFYihongIotContext ef = new EFYihongIotContext();

            bs_language_config_item item = ef.bs_language_config_item.Where(c => c.ci_guid == httpReqModel.guid).FirstOrDefault();
            if (item != null)
            {
                ef.bs_language_config_item.Remove(item);
                ef.SaveChanges();
            }
            return RespResult.Sucess();
        }

        public RespResultData getConfig(HttpReqModel httpRequest)
        {
            return RespResult.Sucess(new DBBsLanguageConfig().getLanguagesConfig(httpRequest.guid));
        }

    }
}